//
//  ReweetedStatusView.swift
//  Sina
//
//  Created by 王杰 on 15/11/16.
//  Copyright © 2015年 王杰. All rights reserved.
//

import UIKit
import SnapKit
class ReweetedStatusView: UIView {

    //转发微博底部约束  用来记录底部约束  后续根据是否有配图 来动态修改
    var bottonConstraints: Constraint?
    //数据模型
    var retweetedStatus:Status? {
    
        didSet{
        
        retweetedLabel.text = retweetedStatus?.text
        self.bottonConstraints?.uninstall()
            
            if let picurls = retweetedStatus?.imageURLs where picurls.count > 0 {
                
            pictureView.imageURLs = picurls
            pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //将约束条件转换成 约束类型
                    self.bottonConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                pictureView.hidden = true
                //将约束条件转换成 约束类型
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //将约束条件转换成 约束类型
                    self.bottonConstraints = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    
    
    //重写父类的构造方法
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.85, alpha: 1)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置UI
    private func setupUI() {
    
    addSubview(retweetedLabel)
        addSubview(pictureView)
        
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(retweetedLabel.snp_left)
//            make.height.equalTo(100)
//            make.width.equalTo(100)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.bottonConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }

    
    }
    
    //懒加载控件
    private lazy var retweetedLabel :UILabel = UILabel(title: "我是转发微博", size: 14, color: UIColor.darkGrayColor(), margin: StatusCellMargin)
    
    private lazy var pictureView: StatusPictureView = StatusPictureView()
}
